from ConfigParser import SafeConfigParser
from Pacx import *
from Person import *
from Util import *
import getopt
import os
import shutil
import subprocess
import sys
import time
import traceback
from urlparse import urlparse
import os.path
import urllib

class MaintainerManager(Pacx):
    
    def __init__(self):
        Pacx.__init__(self)
        self.logpath = self.config.get("maintainers", "logfile")
        self.reference_url = self.config.get("maintainers", "url")
        self.maintainer_ids_file = self.config.get("maintainers", "maintainer_ids_file")
        
    def get_reference_list(self):
        f = urllib.urlopen(self.reference_url)
        out = f.read().strip()
        lines = out.split("\n")
        project_maintainer_map = {}
        for line in lines:
            projmaint = line.split()
            project_maintainer_map[projmaint[0]] = projmaint[1]
        return project_maintainer_map

    
    def get_email_maintainer_map(self):
        maint_ids_content = read_as_string(self.maintainer_ids_file).strip()
        maint_ids = maint_ids_content.split("\n")
        email_maintainer_map = {}
        for maint_id in maint_ids:
            attributes = maint_id.split(",")
            email_maintainer_map[attributes[0]] = Person(attributes[1], attributes[0], attributes[2], attributes[3])
        #email_maintainer_map["nomaintainer@mandriva.com"] = Person("nomaintainer", "", "", "")
        return email_maintainer_map

    def update_wiki_pages(self, project_maintainer_map, email_maintainer_map):
        for k, v in project_maintainer_map.iteritems():
            try:
                self.update_wiki_page(k, email_maintainer_map[v])
            except KeyError, e:
                pass
        
    def update_wiki_page(self, project, maintainer):
        print ""
        #http://doc4.mandriva.org/rest/wikis/xwikidoc4/spaces/wiki/pages/libnepomuk4/objects/doc4.SeeAlso/0/

def main():
    m = MaintainerManager()
    #project_maintainer_map = m.get_reference_list()
    email_maintainer_map = m.get_email_maintainer_map()
    counter = 1
    print email_maintainer_map
    for k, v in email_maintainer_map.iteritems():
        print "%d,%s,%s,%s,%s" % (counter, v.id, v.email, v.pseudo, v.fullname)
        counter += 1
    
    #m.update_wiki_pages(project_maintainer_map, email_maintainer_map) 
    

#select distinct(packager) from PACKAGES;
#for packager in packagers
# select count(package) from PACKAGES where packager=''

#    try:
#        opts, args = getopt.getopt(sys.argv[1:], "hce", ["help", "count-jobs", "extract"])
#    except getopt.GetoptError:
#        pass
#        
#    for opt, arg in opts:
#        if opt in ("-h", "--help"):
#            print 'Usage :'
#            sys.exit(0)
#        elif opt in ("-c", "--count-jobs"):
#            exx = MaintainerManager()
#            exx.db.close()
#        elif opt in ("-e", "--extract"):
#            exx = MaintainerManager()
#            exx.db.close()
        
if __name__ == "__main__":
    main()
